/*eslint-disable*/ 
import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    info:{}
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_INFO: (state, data) => {
    state.info = data
  }
  
}

const actions = {
 async userLogin({commit},data){
    let res= await login(data)
    if(res.code==200) {
     
      commit('SET_TOKEN',res.token)
      setToken(res.token)
    }
  }
  ,

  // get user info
 async getInfo({ commit }) {
    let res = await getInfo()
    
    commit('SET_INFO',res.data)
    const {id,username,nickname,phone,role,date,photo,gender} = res.data
      let data ={}
      data.username = username
      data.id = id
      data.nickname = nickname
      data.phone = phone
      data.role = role
      data.date = date
      data.photo = photo
      data.gender = gender
      localStorage.setItem('infomation',JSON.stringify(data))
    
  },

  


  // user logout
 async logout({ commit, state }) {
    let res = await logout()
    removeToken()
    state = {}
  },


}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

